﻿/*
extesion method for paging called for collection of pagers

*/

(function (jquery) {
    $.fn.Pager = function (options) {
        if (!this instanceof jQuery)
            return;
        var 
            totalPages = options["TotalPages"], /*TotalPages - number  of pages.*/
            startPage = options["StartPage"], /* wrapped object that represents firstpage */
            currentIndex = options["StartPageIndex"], /* StartItem -first page index */
            FormatAjaxResult = options["FormatAjaxResult"], /*  FormatAjaxResult -function for formatting  ajax 
                                                                call result 
                                                                    this  -  clicked pager 
                                                                    arguments - raw html 
                                                                    returns - formatted html */
            UrlWildcard = options["UrlWildcard"], /* UrlWildcard - function that returns function that formats url 
                                                     for ajax call. Returned function takes as argument new page index 
                                                                    this  - pager collection
                                                                    arguments - no*/
            NewIndex = options["NewIndex"], /*  NewIndex - function that returns page index when new page has been selected 
                                                  this  -  clicked pager 
                                                  arguments - event that has been catch during click on one of pageers*/
            FormatPager = options["FormatPager"],
            container = options["Container"],
            pagerCollection = this; /*Container - wrapped container object for pages  */
        var loadedIndecies = {};
        loadedIndecies[currentIndex] = startPage;
        UrlWildcard = UrlWildcard.call(this);
        this.click(function (event) {
            var newIndex = NewIndex.call(this, event);
            if (newIndex !== -1) {
                loadedIndecies[currentIndex].hide();
                if (loadedIndecies[newIndex] != undefined) {
                    loadedIndecies[newIndex].show();
                    currentIndex = newIndex;

                }
                else {
                    $.get(UrlWildcard.call(this, newIndex),
                    function (html) {
                        var $result = FormatAjaxResult(html);
                        $result.appendTo(container);
                        loadedIndecies[newIndex] = $result;
                        currentIndex = newIndex;
                    }
                 );

                }
                pagerCollection.each(function (i, e) {
                    FormatPager.call($(this), newIndex);
                })

            }
            return false;

        })

    }
})(jQuery);